package com.lanwf.adminsuper.sys.admins;

import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.lanwf.IAdminIndexController;
import com.lanwf.admin.BaseController;
import com.lanwf.admin.utils.Result;
import com.lanwf.adminsuper.sys.utils.MsgKit;
import com.lanwf.adminsuper.sys.utils.SM4;
import com.lanwf.common.model.SysAdmin;
import com.lanwf.common.model.SysAdmins;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Lanwf
 * @Date: 2023/8/17 15:50
 * @Description:
 */
@Path(value = "/super-admin/account",viewPath = "/superadmin/sys/account")
public class AdminsController extends BaseController implements IAdminIndexController {

    @Override
    public void index(){
        render("index.html");
    }

    @Override
    public void paginate() {
        Integer p = getInt("pageNum", 1);
        Integer s = getInt("pageSize", 10);

        String orderByColumn = get("orderByColumn", "");
        String isAsc = get("isAsc", "");

        String username = get("username", "");
        List<Object> param = new ArrayList<>();

        String sql = " from sys_admins where 1=1 ";


        if (StrKit.notBlank(username)) {
            sql += " and username like ? ";
            param.add("%" + username + "%");
        }

        sql += " order by ";

        if (StrKit.notBlank(orderByColumn)) {
            sql += "" + orderByColumn + " " + isAsc;
        }

        Page<SysAdmin> paginate = SysAdmin.dao.paginate(p, s, "select *", sql, param.toArray());


        renderJson(new Result<>(paginate.getTotalRow(), paginate.getList()));
    }

    @Override
    public void add() {
        render("add_edit.html");
    }

    @Override
    public void save() {
        SysAdmins model = getModel(SysAdmins.class, "account");
        String salt = StrKit.getRandomUUID();
        model.setPassword(new SM4().encode(model.getPassword()+salt));
        model.set("create_time",new Date())
                .set("update_time",new Date())
                .set("salt",salt)
                .save();
        renderJson(Ret.ok(MsgKit.SAVE_OK));
    }

    @Override
    public void edit() {
        SysAdmins sysAdmins = SysAdmins.dao.findById(getInt(0));
        sysAdmins.setPassword(new SM4().decode(sysAdmins.getPassword()).replace(sysAdmins.getSalt(),""));
        set("account",sysAdmins);
        render("add_edit.html");
    }

    @Override
    public void update() {
        getFile();
        SysAdmins model = getModel(SysAdmins.class, "account");
        String salt = StrKit.getRandomUUID();
        model.setPassword(new SM4().encode(model.getPassword()+salt));
        model.set("update_time",new Date()).set("salt",salt).update();
        renderJson(Ret.ok(MsgKit.UPDATE_OK));
    }

    @Override
    public void delete() {
        SysAdmins.dao.deleteById(getInt(0));
        renderJson(Ret.ok(MsgKit.DELETE_OK));
    }

    public void usernameUnique() {
        Integer id = getInt("id");
        String username = get("username", "");

        SysAdmins first = null;
        if (id != null) {
            first = SysAdmins.dao.findFirst("select * from sys_admins where id != ? and username = ? ", id, username);

        } else {
            first = SysAdmins.dao.findFirst("select * from sys_admins where username = ? ", username);

        }
        if (first != null) {
            renderJson(Ret.fail("账户已重复"));
            return;
        }

        renderJson(Ret.ok("msg", "账户可用"));
    }

}
